package com.wanda.service.impl;

import com.wanda.common.ResponseEnum;
import com.wanda.entity.*;
import com.wanda.exception.Appexception;
import com.wanda.mapper.*;
import com.wanda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sky
 * @date 2022-08-26
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private ScreenMapper screenMapper;
    @Autowired
    private CinemaMapper cinemaMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private TicketMapper ticketMapper;

    /**
     * 根据时间查询哪些电影正在热映
     * @param date
     * @return
     */
    @Override
    public List<Movie> selectMoviesByDate(Date date) {
        MovieExample example = new MovieExample();
        example.createCriteria().andReleaseTimeLessThanOrEqualTo(date).andOffshelfTimeGreaterThan(date);
        List<Movie> movies = movieMapper.selectByExample(example);
        return movies;
    }
    /**
     * 根据城市ID和电影ID查询出上映了这部电影的电影院列表
     * 根据电影院ID查询每个电影院信息
     */
    @Override
    public List<Cinema> selectCinemaByCityidAndMovieid(String cityName, Long movieId) {
        /**
         * 根据城市名字找到城市id
         */
        CityExample cityExample = new CityExample();
        cityExample.createCriteria().andNameLike('%' + cityName + '%');
        List<City> cities = cityMapper.selectByExample(cityExample);
        if (cities == null || cities.size() == 0){
            throw new Appexception(ResponseEnum.CITY_NOT_FOUND);
        }
        Long cityId = cities.get(0).getId();
        /**
         * 根据电影名字找到电影id
         */
//        MovieExample movieExample = new MovieExample();
//        movieExample.createCriteria().andNameLike('%' + movieName + '%');
//        List<Movie> movies = movieMapper.selectByExample(movieExample);
//        if (movies == null || movies.size() == 0){
//            throw new Appexception(ResponseEnum.MOVIE_NOT_FOUND);
//        }
//        Long movieId = movies.get(0).getId();
        /**
         * 根据城市id和电影id查询电影院
         */
        ArrayList<Cinema> cinemas = new ArrayList<>();
        ScreenExample screenExample = new ScreenExample();
        screenExample.createCriteria().andCityIdEqualTo(cityId).andMovieIdEqualTo(movieId);
        List<Screen> screenList = screenMapper.selectByExample(screenExample);
        if (screenList == null || screenList.size() == 0){
            throw new Appexception(ResponseEnum.SCREEN_NOT_FOUNT);
        }
        for (Screen screen : screenList) {
            Long cinemaId = screen.getCinemaId();
            Cinema cinema = cinemaMapper.selectByPrimaryKey(cinemaId);
            cinemas.add(cinema);
        }
        List<Cinema> cinemaList = cinemas.stream().distinct().collect(Collectors.toList());
        return cinemaList;
    }
    @Override
    /**
     * 根据电影院，电影，当前时间查询今天该电影在该影院的场次
     * @param cinemaId 电影院id
     * @param movieId 电影id
     * @param date 当前日期
     * @return
     */
    public List<Screen> selectScreenByExample(Long cinemaId, Long movieId, Date date) {
        ScreenExample screenExample = new ScreenExample();
        screenExample.createCriteria().andCinemaIdEqualTo(cinemaId).andMovieIdEqualTo(movieId).
                andStartTimeGreaterThan(date);
        List<Screen> screens = screenMapper.selectByExample(screenExample);
        if (screens == null || screens.size() == 0){
            throw new Appexception(ResponseEnum.SCREEN_NOT_FOUNT);
        }
        return screens;
    }



    /**
     * 根据场次ID查询影厅
     * @param screenId
     * @return
     */
    @Override
    public Integer selectHallIdByScreenId(Long screenId) {
        int hallId = screenMapper.deleteByPrimaryKey(screenId);
        return hallId;
    }

    /**
     * 查询该场次的票
     * @param screenId
     * @return
     */
    @Override
    public List<Ticket> selectTicketByScreenId(Long screenId) {
        TicketExample ticketExample = new TicketExample();
        ticketExample.createCriteria().andScreenIdEqualTo(screenId);
        List<Ticket> tickets = ticketMapper.selectByExample(ticketExample);
        return tickets;
    }

    /**
     * 买票
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTickets(Long screenId, List<String> siteIds) {
        System.out.println(siteIds);
        ArrayList<Ticket> ticketList = new ArrayList<>();
        for (String siteId : siteIds) {
            System.out.println("---------------");
            TicketExample ticketExample = new TicketExample();
            ticketExample.createCriteria().andScreenIdEqualTo(screenId).andSiteIdEqualTo(new Long(siteId));
            List<Ticket> tickets = ticketMapper.selectByExample(ticketExample);
            System.out.println(tickets);
            Ticket ticket = tickets.get(0);
            System.out.println(ticket);
            Long version = ticket.getVersion();
            if (version == 0){
                ticketMapper.updateByPrimaryKeySelective(Ticket.builder().id(ticket.getId()).isWicket((byte) 1).version(version + 1).build());
                ticketList.add(ticket);
            }else {
                throw new Appexception(ResponseEnum.BUY_TICKET_FAILURE);
            }
        }
    }

}
